package com.newspublish.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newspublish.system.entity.system.News;
import com.newspublish.system.mapper.NewsMapper;
import com.newspublish.system.service.NewsService;
import com.newspublish.system.utils.result.Result;
import com.newspublish.system.utils.util.GetMassege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper,News> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private GetMassege getMassege;

    @Override
    public Result getAllNews() {
        QueryWrapper<News> queryWrapper=new QueryWrapper();
        queryWrapper.eq("publish_state","已发表");
        List<News> newsList=newsMapper.selectList(queryWrapper);
        return Result.ok(newsList).message("获取所有新闻数据");
    }

    @Override
    public Result addNews(News news) {
        news.setAuthor(getMassege.getUser());
        news.setPublishState("未发表");
        news.setAuditState("待审核");
        int i=newsMapper.insert(news);
        if(i>0){
            return Result.ok(news).message("新闻提交成功 等待神恶化发表");
        }else{
            return Result.fail().message("新闻提交失败");
        }
    }

    @Override
    public Result editNews(News news) {
        String publishState=newsMapper.selectById(news.getId()).getPublishState();
        if("已发表".equals(publishState)){
            return Result.fail().message("禁止修改");
        }
        int i=newsMapper.updateById(news);
        if(i>0){
            return Result.ok(newsMapper.selectById(news.getId())).message("修改新闻成功");
        }else{
            return Result.fail().message("修改新闻失败");
        }
    }

    @Override
    public Result auditNews(News news) {
        int i=newsMapper.auditNews(news);
        //获取更新后的news对象数据 不返回内容字段 保护数据安全
        newsMapper.selectByPrimaryKeyexcludeContent(news);
        if(i>0){
            News news1=newsMapper.selectById(news.getId());
            return Result.ok(news1).message("审核成功");
        }

        return Result.fail().message("审核失败");
    }

    @Override
    public Result deleteNews(List<Long> id) {
        return null;
    }

    @Override
    public Result getByCategory(String category) {
        List<News> newsList=newsMapper.selectByCategory(category,"已发表");
        return Result.ok(newsList).message("获取新闻成功");
    }

    @Override
    public Result getByPrimaryKey(Long id) {
        QueryWrapper<News> queryWrapper=new QueryWrapper();
        queryWrapper.eq("publishState","已发表");
        List<News> newsList=newsMapper.selectList(queryWrapper);
        return Result.ok(newsList).message("获取成功");
    }

    @Override
    public Result like(Long id) {
        newsMapper.like(id);
        return Result.ok().message("点赞+1");
    }

    @Override
    public Result getByAuthor(String author) {
        List<News> newsList=newsMapper.selectByAuthor(author,"已发表");
        return Result.ok(newsList).message("获取成功");
    }

}
